using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;

namespace VoiceAd.DAL
{
    public class SendMailStatusRepository : ISendMailStatusRepository, IDisposable
    {
        private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

        public SendMailStatusRepository(dbVoiceAdDataContext db)
        {
            this.context = db;
        }

        public IEnumerable<tblSendMailStatus> GetListSendMailStatus()
        {
            return context.tblSendMailStatus.ToList();
        }

        public IEnumerable<tblEmailCampaign> GetListEmailByCampaignId(int lCampaignId)
        {
            return context.GetListEmailByCampaignId(lCampaignId);
        }

        public tblSendMailStatus GetSendMailStatusById(int lSendMailStatusId)
        {
            return context.tblSendMailStatus.Where(x => x.Id == lSendMailStatusId).FirstOrDefault();
        }

        public void InsSendMailStatus(tblSendMailStatus tblsendMailStatus)
        {
            context.tblSendMailStatus.InsertOnSubmit(tblsendMailStatus);
            context.SubmitChanges();
        }

        public void UpdSendMailStatus(tblSendMailStatus tblsendMailStatus)
        {
            tblSendMailStatus obj = context.tblSendMailStatus.Where(x => x.Id == tblsendMailStatus.Id).First();
            obj.CampaingId = tblsendMailStatus.CampaingId;
            obj.StationId = tblsendMailStatus.StationId;
            obj.Email = tblsendMailStatus.Email;
            obj.Massage = tblsendMailStatus.Massage;
            context.SubmitChanges();
        }

        public void DelSendMailStatus(int id)
        {
            tblSendMailStatus obj = context.tblSendMailStatus.Single(x => x.Id == id);
            context.tblSendMailStatus.DeleteOnSubmit(obj);
            context.SubmitChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
